﻿using System;
using System.Collections.Generic;
using System.Configuration;
using Entity;
using System.Data;

namespace BLL
{

    /// <summary>
    /// 词语管理
    /// </summary>
    public class WordsManager
    {
    
        #region 属性
        public IWordsManager Instance {
            get {
                if (null == _instance)
                    _instance = GetInstance();
                return _instance;
            }
        }
        #endregion

        #region 变量
        private IWordsManager _instance;
        #endregion

        public WordsManager() { }

        /// <summary>
        /// 词语管理类型
        /// cache:启用缓存;
        /// database:访问数据库 
        /// </summary>
        private static readonly string _type = ConfigurationManager.AppSettings["WordManagerType"].ToString();

        /// <summary>
        /// 获取实例
        /// </summary>
        /// <returns></returns>
        private   IWordsManager GetInstance()
        { 
            if (string.IsNullOrEmpty(_type))
            {
                return null;
            }
            if (_type == "database")
            { 

                return new WordsDataBaseManager();
            }
            else { 
                return new WordsCacheManager();
            } 
        }

        public interface IWordsManager
        {
            /// <summary>
            /// 根据词语编号查找到词语信息
            /// </summary>
            /// <param name="wordsId">词语编号</param>
            /// <returns></returns>
              S_WORDS FindWordsByWordsID(int wordsId);

            /// <summary>
            /// 根据隶属词语编号获取词语信息
            /// </summary>
            /// <param name="belongId">隶属词语编号</param>
            /// <returns></returns>
              List<S_WORDS> FindWordsListByBelongID(int belongId);

            /// <summary>
            /// 根据隶属词语编号获取词语信息
            /// </summary>
            /// <param name="belongId"></param>
            /// <returns></returns>
              DataTable FindWordsDataTableByBelongID(int belongId);

            /// <summary>
            /// 根据隶属词语编号获取词语信息
            /// </summary>
            /// <param name="belongid"></param>
            /// <param name="middleAffix"></param>
            /// <returns></returns>
              DataTable FindWordsDataTableWithMiddleAffixByBelongID(int belongid, string middleAffix);
            /// <summary>
            /// 刷新  非缓存无用
            /// </summary>
             void Refresh();
        }

        /// <summary>
        /// 缓存词语
        /// </summary>
        private class WordsCacheManager : IWordsManager
        {

            #region 属性

            #endregion

            #region 变量
            /// <summary>
            /// 词语缓存
            /// </summary>
            private List<S_WORDS> _wordsCache = null;

            /// <summary>
            /// 词语BLL
            /// </summary>
            private S_WORDSBLL _bll = new S_WORDSBLL();
            #endregion

            #region 方法
            /// <summary>
            /// 根据词语编号查找到词语信息
            /// </summary>
            /// <param name="wordsId">词语编号</param>
            /// <returns></returns>
            public S_WORDS FindWordsByWordsID(int wordsId)
            {
                if (null == _wordsCache) Refresh();
                return null;
            }
            /// <summary>
            /// 根据隶属词语编号获取词语信息
            /// </summary>
            /// <param name="belongId">隶属词语编号</param>
            /// <returns></returns>
            public List<S_WORDS> FindWordsListByBelongID(int belongId)
            {
                throw new NotImplementedException();
            }

            /// <summary>
            /// 根据隶属词语编号获取词语信息
            /// </summary>
            /// <param name="belongId"></param>
            /// <returns></returns>
            public DataTable FindWordsDataTableByBelongID(int belongId)
            {
                throw new NotImplementedException();
            }

            /// <summary>
            /// 根据隶属词语编号获取词语信息
            /// </summary>
            /// <param name="belongid"></param>
            /// <returns></returns>

            public DataTable FindWordsDataTableWithMiddleAffixByBelongID(int belongid, string middleAffix)
            {
                throw new NotImplementedException();
            }

            /// <summary>
            /// 刷新
            /// </summary>
            public void Refresh()
            {
                ClearCache();
                AddCache();
            }


            /// <summary>
            /// 清除缓存
            /// </summary>
            private void ClearCache()
            {
                if (null != _wordsCache && _wordsCache.Count > 0)
                    _wordsCache = null;
            }

            /// <summary>
            /// 添加缓存
            /// </summary>
            private void AddCache()
            {
                var dt = _bll.SelectS_WORDsAll();
                if (null != dt && dt.Rows.Count > 0)
                {
                    _wordsCache = BusinessTool.ConvertDataTable<S_WORDS>(dt);
                }
            }

            #endregion


        }

        /// <summary>
        /// 数据库访问词语
        /// </summary>
        private class WordsDataBaseManager : IWordsManager
        {

            #region 变量
            /// <summary>
            /// 存储过程访问
            /// </summary>
            private  Access.SpHelper _spHelper = new Access.SpHelper();
            /// <summary>
            /// 词语BLL
            /// </summary>
            private    S_WORDSBLL _bll = new S_WORDSBLL();
            #endregion

            /// <summary>
            /// 根据词语编号查找到词语信息
            /// </summary>
            /// <param name="wordsId">词语编号</param>
            /// <returns></returns>
            public  S_WORDS FindWordsByWordsID(int wordsId)
            {
                string whereCondition = " WORDSID=" + wordsId.ToString();
                string orderby = " WORDSID";
               var dt= _bll.SelectS_WORDsDynamic(whereCondition, orderby);
               if (null !=dt&& dt.Rows.Count > 0)
               {
                   return BusinessTool.ConvertToEntity<S_WORDS>(dt);
               }
               else
               {
                   return null;
               }
            }
            /// <summary>
            /// 根据隶属词语编号获取词语信息
            /// </summary>
            /// <param name="belongId">隶属词语编号</param>
            /// <returns></returns>
            public  List<S_WORDS> FindWordsListByBelongID(int belongId)
            {
                string whereCondition = " BELONGID=" + belongId.ToString();
                string orderby = " WORDSID";
                var dt = _bll.SelectS_WORDsDynamic(whereCondition, orderby);
                if (null != dt && dt.Rows.Count > 0)
                {
                    List<S_WORDS> _list = null;
                    for (int i = 0; i < dt.Rows.Count; i++) {

                        if(null==_list) _list=new List<S_WORDS>();
                        _list.Add(BusinessTool.ConvertDataRow<S_WORDS>(dt.Rows[i]));

                    }
                    if (_list.Count > 0)
                        return _list;
                    else
                        return null;
                }
                else
                {
                    return null;
                }
            }
            /// <summary>
            /// 根据隶属词语编号获取词语信息
            /// </summary>
            /// <param name="belongid"></param> 
            /// <returns></returns>
            public  DataTable FindWordsDataTableByBelongID(int belongId)
            {
                var ds = _spHelper.P_GETCONTENTBYWORDS(99, string.Empty, belongId);
                return null == ds ? null : ds.Tables[0];
            }
            /// <summary>
            /// 根据隶属词语编号获取词语信息
            /// </summary>
            /// <param name="belongid"></param>
            /// <param name="middleAffix"></param>
            /// <returns></returns>
            public  DataTable FindWordsDataTableWithMiddleAffixByBelongID(int belongid, string middleAffix)
            {
                var ds = _spHelper.P_GETCONTENTBYWORDS(0, middleAffix, belongid); 
                return null==ds?null:ds.Tables[0];
            }
          

            public  void Refresh()
            {
                
            }
             
        }
     
    }


}
